type NetHandler = (actor: any, net_pack: any) => void;
class NetDispatcher {
    private dispatcher: NetHandler[][] = [];
    NetDispatcher() {
        this.dispatcher = new Array();
        _G["OnNetMsg"] = (...args: [number, number, any, any]) => this.OnNetMsg(...args); // 注册全局函数["OnNetMsg
    }
    /**
     * 注册处理器函数
     *
     * @param proc 处理器函数
     * @param system_id 系统ID
     * @param msg_id 消息ID
     * @returns 注册成功返回true，否则返回false
     */
    public reg(proc: NetHandler,system_id: number, msg_id: number): boolean {
        if(proc == null) {
            return false;
        }
        if (this.dispatcher[system_id] == null) {
            this.dispatcher[system_id] = [];
        } 
        this.dispatcher[system_id][msg_id] = proc;
        System.regScriptNetMsg(system_id, msg_id);
        return true;
    }

    /**
     * 处理网络消息的方法
     *
     * @param system_id 系统ID
     * @param msg_id 消息ID
     * @param actor 消息接收者
     * @param net_pack 网络数据包
     */
    public OnNetMsg(system_id: number, msg_id: number, actor:any,net_pack:any): void {
        if (this.dispatcher[system_id] == null) {
            console.warn(`未注册的消息处理器：system=${system_id}, msg=${msg_id}`);
            return;
        }
        let procs = this.dispatcher[system_id][msg_id];
        if (procs == null) {
            return;
        }
        procs(actor,net_pack);
    }
}

export = new NetDispatcher();